//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MidPointCdsEngine.h"
using namespace Cephei::QL::Pricingengines::Credit;
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CDefaultProbabilityTermStructure^ _Cprobability;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phMidPointCdsEngine = NULL;
#endif
        _Cprobability = safe_cast<CDefaultProbabilityTermStructure^> (probability);
        _Cprobability->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _probability = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_Cprobability->GetHandle ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate); //d
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        boost::optional<bool> _includeSettlementDateFlows;
        if (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeSettlementDateFlows))
        {
            boost::optional<bool> t = boost::optional<bool> (ValueHelper::Convert (includeSettlementDateFlows->Value)); //e
            _includeSettlementDateFlows = boost::optional<boost::optional<bool>> (t);
        }
        else
            _includeSettlementDateFlows = boost::none;
        _ppMidPointCdsEngine = new boost::shared_ptr<QuantLib::MidPointCdsEngine> (new QuantLib::MidPointCdsEngine ( _probability,  _recoveryRate,  _discountCurve,  _includeSettlementDateFlows ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprobability != nullptr) _Cprobability->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (boost::shared_ptr<QuantLib::MidPointCdsEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMidPointCdsEngine = NULL;
#endif
	_ppMidPointCdsEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (QuantLib::MidPointCdsEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMidPointCdsEngine = NULL;
#endif
	_ppMidPointCdsEngine = new boost::shared_ptr<QuantLib::MidPointCdsEngine> (&childNative);
    
    _MidPointCdsEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (CMidPointCdsEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMidPointCdsEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMidPointCdsEngine = new boost::shared_ptr<QuantLib::MidPointCdsEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMidPointCdsEngine = NULL;
#endif
	if (!t->IsSubclassOf(CMidPointCdsEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (QuantLib::Handle<QuantLib::MidPointCdsEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMidPointCdsEngine = &childNative;
	_ppMidPointCdsEngine = &static_cast<boost::shared_ptr<QuantLib::MidPointCdsEngine>>(childNative.currentLink());
    
    _MidPointCdsEngineOwner = owner;
}
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (QuantLib::Handle<QuantLib::MidPointCdsEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMidPointCdsEngine = &childNative;
	_ppMidPointCdsEngine = &static_cast<boost::shared_ptr<QuantLib::MidPointCdsEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::CMidPointCdsEngine (QuantLib::MidPointCdsEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMidPointCdsEngine = NULL;
#endif
	_ppMidPointCdsEngine = new boost::shared_ptr<QuantLib::MidPointCdsEngine> (new QuantLib::MidPointCdsEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::~CMidPointCdsEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMidPointCdsEngine != NULL)
    {
	    delete _ppMidPointCdsEngine;
        _ppMidPointCdsEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::!CMidPointCdsEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMidPointCdsEngine != NULL)
    {
	    delete _ppMidPointCdsEngine;
    }
}
QuantLib::MidPointCdsEngine& Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::GetReference ()
{
    if (_ppMidPointCdsEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppMidPointCdsEngine;
}
boost::shared_ptr<QuantLib::MidPointCdsEngine>& Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::GetShared ()
{
    if (_ppMidPointCdsEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppMidPointCdsEngine;
}
QuantLib::MidPointCdsEngine* Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::GetPointer ()
{
    if (_ppMidPointCdsEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppMidPointCdsEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MidPointCdsEngine>& Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::GetHandle ()
{
	if (_phMidPointCdsEngine == NULL)
	{
		_phMidPointCdsEngine = new Handle<QuantLib::MidPointCdsEngine> (*_ppMidPointCdsEngine);
	}
	return *_phMidPointCdsEngine;
}
#endif
bool Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::HasNative () 
{
	return (_ppMidPointCdsEngine != NULL);
}

Cephei::QL::Pricingengines::Credit::IMidPointCdsEngine^ Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine::Calculate::get ()
{
    try
    {
    	(*_ppMidPointCdsEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Credit::IMidPointCdsEngine^ Cephei::QL::Pricingengines::Credit::CMidPointCdsEngine_Factory::Create (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeSettlementDateFlows)
{
    return REFNEW CMidPointCdsEngine ( probability,  recoveryRate,  discountCurve,  includeSettlementDateFlows);
}
